package com.rainkaze.lifeharbor;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.rainkaze.lifeharbor.fragment.*;

import java.util.ArrayList;
import java.util.List;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {

    List<Fragment> list;
    BottomNavigationView bottomNavigationView;
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        bottomNavigationView = findViewById(R.id.bottom_navigation);

        list = new ArrayList<>();
        list.add(new CollectItemFragment());
        list.add(new AllItemFragment());
        list.add(new RandomItemFragment());

        if (savedInstanceState == null) {
            bottomNavigationView.setSelectedItemId(R.id.menu_item_collect);
        }


        if (bottomNavigationView != null) {
            bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;
                    int itemId = item.getItemId();
                    if (itemId == R.id.menu_item_collect) {
                        selectedFragment = list.get(0);
                    } else if (itemId == R.id.menu_item_all) {
                        selectedFragment = list.get(1);
                    } else if (itemId == R.id.menu_item_random) {
                        selectedFragment = list.get(2);
                    }

                    if (selectedFragment != null) {
                        showFragment(selectedFragment);
                    }
                    return true;
                }
            });

            if (savedInstanceState == null && list.size() > 1) {
                if (bottomNavigationView.getSelectedItemId() == R.id.menu_item_all){
                    showFragment(list.get(1));
                } else {
                    showFragment(list.get(0));
                }
            } else if (savedInstanceState == null) {
                showFragment(list.get(0));
            }

        }

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBarInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBarInsets.left, systemBarInsets.top, systemBarInsets.right, 0);
            return insets;
        });
    }

    private void showFragment(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fragment_container, fragment);
        ft.commit();
    }
}